$(".header-sub-menu").css("width", $(window).width());
let loginId = localStorage;
$(".heder-top-text").mouseover(function() {
    $(".header-promotion").css({ "display": "block" })
    $(".header-promotion").stop();
    $(".header-promotion").animate({
        width: 310,
        height: 260,
        right: -100,
        opacity: 1
    }, 300)
    $(".header-promotion").mouseover(function() {
        $(".header-promotion").css({ "display": "block" })
        $(".header-promotion").stop();
    });
});
let whether = 0;
$(".heder-top-text").mouseout(function() {
    $(".header-promotion").stop();
    $(".header-promotion").animate({
        width: 200,
        height: 120,
        right: -50,
        opacity: 0
    }, 50);
    $(".header-promotion").css({
        "width": 0,
        "hight": 0,
        "display": "none"
    })
});
$(document).ready(function() {
    // $("header").load("header.html");
    // $("footer").load("footer.html");
    let loginId = localStorage;

    $(".header-client-login").click(function(event) {
        $(".footer-client-message").css("display", "flex");
        event.stopImmediatePropagation();
        console.log("通过");
        $(document).click(function() {
            $(".footer-client-message").css("display", "none");
            $(document).unbind("click");
        });
    });
    $(document).click(function() {
        $(".footer-client-message").css("display", "none");
        $(document).unbind("click");
    });
    $(".header-quit").click(function() {
        console.log($(this))
        $(".header-client-login").css("display", "none")
        $(".heder-login-register").css("display", "block");
        $(".header-shopp-sum").css("display", "none")
            // $.cookie('name',null);
            // localStorage.removeItem(key);
        loginId.removeItem("shoppGoodsSum");
        loginId.removeItem("name");
    })


    if (loginId.name) {
        // alert(loginId.name);
        $(".heder-login-register").css("display", "none")
        console.log($(".header-login-id").html(), $(".header-client-login"))
            // 账号
        $(".header-client-login").css("display", "block")
        $(".header-login-id").html(loginId.name);
        // console.log("通过");
        // 购物车数量
        // if (logined.ifShoppGoods) {

        // }


        // 获取头部购物车数量
        $.ajax({
            type: "GET",
            url: "php/toGoods.php",
            data: "logId=" + loginId.name,
            success: function(msg) {
                // msg = msg.slice(1, msg.length - 2);
                // loginId.shoppGoodsSum = msg;
                // // 转json对象
                // // msg = JSON.parse(msg);
                // // localStorage转json对象
                // console.log(JSON.parse(loginId.shoppGoodsSum));
                goodsAndNum(msg);

            }
        })

        // 头部购物车数量
        function goodsAndNum(msg) {
            loginId.shoppGoodsSum = msg;
            // msg = JSON.parse(msg);
            // localStorage转json对象

            // console.log(shoppGoodsNum);
            // $.post("php/goods.php").then(function(resText,status){
            //     console.log(resText);
            //     console.log(status);
            // });
            if (loginId.shoppGoodsSum != 0) {
                console.log(JSON.parse(loginId.shoppGoodsSum));
                let allShoppGoods = JSON.parse(loginId.shoppGoodsSum);

                let clientShoppGoodsNum = 0;
                // 遍历json长度赋值给购物车数量
                for (let index in allShoppGoods) {
                    // console.log(allShoppGoods[index]);
                    clientShoppGoodsNum++;
                }
                $(".header-shopp-sum").css("display", "block");
                // 加载购物车数据
                

                for(let i = 0;i<clientShoppGoodsNum/5&&i<3;i++){

                    // 求得当前商品的价钱
                    let goodsmMoney = JSON.parse(loginId.goods);
                    // console.log(allShoppGoods);
                    let s = allShoppGoods["a" + i];
                    // console.log(s);
                    s = s.charAt(3);
                    Money = goodsmMoney[s + "money"];
                    console.log(goodsmMoney[s + "money"])
                    let htmlText = $(".header-goodsing").html()+"<div class="+"header-goods-message"+"><div class="+"goods-little-img"+"><img class="+"header-small-goods-one"+" src="+"imgs-goods/"+allShoppGoods["a" + i] + "0.jpg"+" alt="+"></div><p class="+"header-goods-name"+">"+allShoppGoods["d" + i]+"</p><p class="+"header-goods-money"+">"+Money+"</p></div>";
                    $(".header-goodsing").html(htmlText);
                }

                $(".header-shopp-sum").html(clientShoppGoodsNum / 5);
                // console.log(clientShoppGoodsNum);
                // funSum();
            }
        }

    }

    // function loadGoods(msg) {
    //     // console.log(JSON.parse(msg));
    //     // let local = localStorage;

    //     applyGoods();

    //     // console.log(msg.length)
    // }
    if (loginId.goods == undefined) {
        console.log("通过")
        $.ajax({
            type: "GET",
            url: "php/goods.php",
            success: function(msg) {
                loginId.goods = msg;
            }
        })
    } else {
        console.log("以获取物品信息")
    }



    function funSum() {
        if (loginId.shoppGoodsSum) {
            console.log(loginId.length)
            $(".header-shopp-sum").css("display", "block");
            $(".header-shopp-sum").html(loginId.length + 1);
        }
    }

})
$(".header-top-goods-box").mouseover(function() {
    if ($(".header-shopp-sum").css("display") == "block") {
        $(".header-shopping-box").stop().css("display", "block").animate({
            opacity: 1
        })
    }

})
$(".header-top-goods-box").mouseout(function() {
        $(".header-shopping-box").stop().animate({
            opacity: 0
        }, function() {
            $(".header-shopping-box").css("display", "none")
        })
    })
    // 账号未登录得时候跳转到登录页
$(".header-top-shoop").click(function() {
    
    let loginId = localStorage;
    console.log(loginId.name);
    if(loginId.name){
        $(location).attr('href', 'Shopping-cart.html')
        return false;
    }else{
       $(location).attr('href', 'log.html')
       return false; 
    }
    // return false; 
})
// for(let)
// for(let i = 0 ;i<$(".header-sub-menu a").length;i++){
//     if($(".header-sub-menu a").attr("href"))
// }
$(".header-sub-menu a").attr("href","commodityPage.html");